// Number.cpp: implementation of the CNumber class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Number.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNumber::CNumber()
{
	m_TextColor = RGB(255,255,255) ;	
}

CNumber::~CNumber()
{

}

BOOL CNumber::Create(CString file,CString files,UINT id,UINT ids)
{
	BOOL bRet = FALSE ;
	if( file != "" )
		bRet = m_Dib.AttachMapFile(file) ;
	else if(id != 0 )
		bRet = m_Dib.LoadResource(id);
	if( bRet == FALSE )
		return FALSE ;
	m_size = m_Dib.GetDimensions();
	if( m_size.cx % 11 != 0)
		return FALSE ;
	m_size.cx = m_size.cx/11 ;

	if( files != "" )
		bRet = m_Dibs.AttachMapFile(files) ;
	else if(ids != 0 )
		bRet = m_Dibs.LoadResource(ids);
	if( bRet == FALSE )
		return FALSE ;

	m_sizes = m_Dibs.GetDimensions();
	if( m_sizes.cx % 11 != 0)
		return FALSE ;
	m_sizes.cx = m_sizes.cx/11 ;

	return TRUE ;
}

void CNumber::DrawMinus(CDC* pDC, int x , int y)
{
	CPoint p(x,y) ;	
	CPoint ps(0,0) ;
	ps.x = 10*m_size.cx ;
	m_Dib.TransparentDraw(pDC,p,m_size,ps,m_size,NTP_COLOR);
	if( m_TextColor != NTX_COLOR)
	{		
		for(int i = 0 ; i < m_size.cx ; i++ )
			for(int j = 0 ; j < m_size.cy ; j++ )
				if( pDC->GetPixel(x+i,y+j) == NTX_COLOR)
					pDC->SetPixel(x+i,y+j,m_TextColor) ; 			
	}
}

CSize CNumber::DrawNumber(CDC* pDC , BOOL bS,int number,int x , int y ) 
{
	CPoint p(x,y) ;	
	CPoint ps(0,0) ;
	CDib* pDib ;
	CSize size ;
	if(bS)
	{
		pDib = &m_Dibs;
		size = m_sizes ;
	}
	else
	{
		pDib = &m_Dib;
		size = m_size ;
	}

	int count = 0;
	if( number < 0 )
	{
		ps.x = 10*size.cx ;
		pDib->TransparentDraw(pDC,p,size,ps,size,NTP_COLOR);
		count++;
		p.x += size.cx ;
		number = (-1)*number ;
	}

	int k = 1 ;
	int t = number ;
	while( t /10 )
	{
		k *= 10 ;
		t /= 10 ;
	}
	
	while( k )
	{
		t = number/ k ;
		ps.x = t*size.cx;
		pDib->TransparentDraw(pDC,p,size,ps,size,NTP_COLOR);
		count++;
		p.x += size.cx ;
		number %= k ;
		k /= 10 ;			
	}
	CSize rsize ;
	rsize.cx = size.cx*count ;
	rsize.cy = size.cy ;
	if( m_TextColor != NTX_COLOR)
	{		
		for(int i = 0 ; i < rsize.cx ; i++ )
			for(int j = 0 ; j < rsize.cy ; j++ )
				if( pDC->GetPixel(x+i,y+j) == NTX_COLOR)
					pDC->SetPixel(x+i,y+j,m_TextColor) ; 			
	}
	return rsize ;
}

void CNumber::Draw(CDC* pDC , int number,int x , int y )
{
	DrawNumber(pDC,FALSE,number,x,y);
}
void CNumber::Draw(CDC* pDC , double var,int sc,int x , int y ) 
{
	int number = (int)var ;
	CSize s = DrawNumber(pDC,FALSE,number,x,y);
	if( sc > 0 )
	{
		x += s.cx ;
		y += (m_size.cy-m_sizes.cy); 
		double db = var-number ;		
		for(int i=0 ; i < sc ; i++)
		{
			number = (int)(db*10+0.0005) ;
			DrawNumber(pDC,TRUE,number,x,y);
			x += m_sizes.cx ;
			db = db*10-number ;		
		}		
	}
}

CSize CNumber::GetSize(int number)
{
	CSize ss = m_size ;
	int k = 1 ;
	int t = number ;
	while( t /10 )
	{
		k++ ;
		t /= 10 ;
	}
	ss.cx = k*m_size.cx ;
	return ss ;
}

CSize CNumber::GetSize(double var,int sc)
{
	CSize ss =GetSize(int(var)) ;
	ss.cx += sc*m_sizes.cx ;
	return ss ;
}
